﻿/********************************************************
// Module Name    : Run Time Show Window
// Purpose        : Main Application Window.
                    Constructs, initializes and positions main Application panel (Apanel)
                    Gets content, menu, title from APanel
// Class Used     : GlobalVariable.cs, CommonFunctions.cs,Context.cs
// Created By     : Harwinder 
// Date           : 
**********************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Panels;
using System.ComponentModel;
using VAdvantage.Classes;
using VAdvantage.Grids;
using System.Threading;
using System.Data;
using VAdvantage.DataBase;

namespace VAdvantage.Apps
{
    public class AWindow : CFrame
    {
        public AWindow()
            : base()
        {
            this.ContentGrid.Children.Add(_aPanel);
            VAdvantage.Utility.Envs.RegisterWindowForm(this);

        }

        #region "Declaration"
        //Main Panel view
        private APanel _aPanel = new APanel();
        /*	Logger	*/
        private static Logging.VLogger log = Logging.VLogger.GetVLogger(typeof(AWindow).FullName);

        //Event raie after window intilize
        public event EventHandler Initialized;

        #endregion

        protected override void Dispose(bool disposing)
        {
            if (Utility.Envs.HideWindow(this))
            {
                return;
            }
            //this.Close();

            //  log.Info(ToString());
            if (_aPanel != null)
                _aPanel.Dispose();
            _aPanel = null;
            // this.removeAll();
            //base.Dispose();
            base.Dispose(disposing);
        }


        public bool InitWorkbench(int AD_Workbench_ID)
        {
            this.Name = "AWindow_WB_" + AD_Workbench_ID;
            bool loadedOK = _aPanel.InitPanel(AD_Workbench_ID, 0, null, this);

            //  CommonInit();
            return loadedOK;
        }   // 


        BackgroundWorker worker;

        /// <summary>
        ///Dynamic Initialization Single Window
        /// </summary>
        /// <param name="AD_Window_ID"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool InitWindow(int AD_Window_ID, Query query)
        {
            //this.Name = "AWindow_" + AD_Window_ID;
            SetAD_Window_ID(AD_Window_ID);
            //
            int wt;
            int ct;

            //ThreadPool.SetMinThreads(50, 50);


            worker = new BackgroundWorker();

            worker.RunWorkerCompleted += (sender, e) =>
            {
                if ((bool)e.Result)
                {
                    CommonInit();

                    worker = null;
                    if (Initialized != null)
                    {
                        Initialized(this, EventArgs.Empty);
                        return;
                    }
                    _aPanel.ShowWindow();
                    //log.Info("Show <===>" + DateTime.Now.TimeOfDay.ToString());

                }
                else
                {
                    Utility.Envs.SetBusyIndicator(false);
                }
            };

            worker.DoWork += (sender, e) =>
            {
                e.Result = _aPanel.InitPanel(0, AD_Window_ID, query, this);
                //_aPanel.InitPanel2(this);
            };

            worker.RunWorkerAsync();
            Utility.Envs.SetBusyIndicator(true);
            return true;
        }

        /// <summary>
        /// Common Init.
        /// Set Image and title
        /// </summary>
        private void CommonInit()
        {
            /* Set Menu strip items */
            // this.SetMenuBar(_aPanel.GetMenuBar());
            //this.GetMenuPane().Controls.Add(_aPanel.GetMenuBar());
            /* Set Title */
            this.SetTitle(_aPanel.GetTitle());
            //
            System.Windows.Media.Imaging.BitmapImage image = _aPanel.GetImage();
            if (image != null)
            {
                SetIcon(image);
                _aPanel.SetWindowImage(image);
            }
        }

        //Show window
        public void ShowWindow()
        {
            this.Width = this.Explorer.ActualWidth-20;
            this.Height = this.Explorer.ActualHeight-20-35;
            this.Left = 10;
            this.Top = 10;
            this.Show();
           // this.FormState = SilverlightDesktop.Common.FormState.Maximized;
            //_host.ShowBusyIndicator = false;
        }

        /// <summary>
        /// Show window( call from Arequest etc)
        /// </summary>
        public void ShowAPanel()
        {
            _aPanel.ShowWindow();
            //_host.ShowBusyIndicator = false;
        }

        /// <summary>
        /// Get window id against window name
        /// </summary>
        /// <param name="winName"></param>
        /// <returns></returns>

        public int GetWindowID(string winName)
        {
            int AD_WINDOW_ID = 0;
            string sql = "SELECT AD_WINDOW_ID FROM AD_WINDOW WHERE upper(NAME) = upper('" + winName + "')";
            IDataReader dr = DB.ExecuteReader(sql);
            while (dr.Read())
            {
                AD_WINDOW_ID = int.Parse(dr[0].ToString());
            }
            dr.Close();

            return AD_WINDOW_ID;
        }

        /// <summary>
        ///Get Application Panel
        /// </summary>
        /// <returns>panel</returns>
        public APanel GetAPanel()
        {
            return _aPanel;
        }
    }
}
